package com.gqz.aircraftwar.mvp.main

import android.content.Intent
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import com.gqz.aircraftwar.App
import com.gqz.aircraftwar.databinding.ActMainBinding
import com.gqz.aircraftwar.mvp.model.aircraft.EnemyManager
import com.gqz.aircraftwar.mvp.view.EnemyPath
import com.gqz.aircraftwar.util.*
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.coroutines.*
import kotlin.system.measureTimeMillis

/**
 * Descraption
 *
 * @author gqz
 * @data 2020/3/23
 */

class MainPresenter(view: MainConstract.IView) : MainConstract.IPresetner {

    private val TAG: String = "MainPresenter-->"

    private var view: MainConstract.IView
    private lateinit var binding: ActMainBinding

    lateinit var sp: Preference

    init {
        this.view = view
        view.setIPresenter(this)
    }

    override fun <T> setBinding(binding: T) {
        this.binding = binding as ActMainBinding
    }

    override fun onResume(owner: LifecycleOwner) {
        i(TAG, "onResume-->")
    }

    override fun onPause(owner: LifecycleOwner) {
        i(TAG, "onPause-->")
        EnemyPath.reset()
        runBlocking { job?.cancelAndJoin() }
        i(TAG, "onDestroy--> job.isCompleted=${job?.isCompleted}")
    }

    override fun onLifecycleChanged(owner: LifecycleOwner, event: Lifecycle.Event) {
        i(TAG, "onLifecycleChanged--> ${event.name}")
    }

    override fun initPlayerInfo(intent: Intent) {
        var fileName = intent.getStringExtra("player")
        sp = Preference(fileName)
        i(TAG, "fileName=$fileName , player=${sp.get("name", "PlayerDef")}")

    }

    override fun startGame() {
        setWave1()
    }

    var job: Job? = null

    fun setWave1() {
        var checkpoint = 1
        job = GlobalScope.launch {
            i(TAG, "start--> presenter.setWave1() worked in ${Thread.currentThread().name}")
            i(TAG, "setWave1--> checkpoint=$checkpoint")
            if (checkpoint <= 1) {
                EnemyManager.instance.generateGroup(5, EnemyPath.xPath)
                checkpoint++
            }
            while (!EnemyManager.instance.isAllDead) {
                i(TAG, "setWave1--> interval")
                delay(2000)
            }
            i(TAG, "setWave1--> checkpoint=$checkpoint")
            if (checkpoint <= 2) {
                EnemyManager.instance.generateGroup(7, EnemyPath.sPath)
            }
            while (!EnemyManager.instance.isAllDead) {
                i(TAG, "setWave1--> interval")
                delay(2000)
            }
            i(TAG, "setWave1--> checkpoint=$checkpoint")
            if (checkpoint <= 3) {
                EnemyManager.instance.generateGroup(9, EnemyPath.uPath)
                checkpoint++
            }
            while (!EnemyManager.instance.isAllDead) {
                delay(2000)
            }
            if (checkpoint <= 4) {
                EnemyManager.instance.generate(MyEnum.EnemyType.boss, EnemyPath.bPath)
                delay(2000)
            }
            while (!EnemyManager.instance.isAllDead) {
                delay(2000)
                if (EnemyPath.bPath.moveEnd) {
                    EnemyPath.bPath.reset()
                }
            }
            LiveEventBus.get(App.EventKey.GameStatus_WaveOver, String::class.java)
                .post("第1关完成！")
            i(TAG, "setWave1-->end")
        }
    }

}
